<template><div><h2 id="命令行测试" tabindex="-1"><a class="header-anchor" href="#命令行测试"><span>命令行测试</span></a></h2>
<p>Laravel 10 中文文档 /</p>
<h2 id="控制台测试" tabindex="-1"><a class="header-anchor" href="#控制台测试"><span>控制台测试</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#success-failure-expectations">期望成功/失败</a></li>
<li><a href="#input-output-expectations">期望输入/输出</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>除了简化 HTTP 测试之外，Laravel 还提供了一个简单的 API 来测试应用程序的 <a href="https://learnku.com/docs/laravel/10.x/artisan" target="_blank" rel="noopener noreferrer">自定义控制台命令</a>。</p>
<h2 id="期望成功-失败" tabindex="-1"><a class="header-anchor" href="#期望成功-失败"><span>期望成功/失败</span></a></h2>
<p>首先，让我们探索如何对 Artisan 命令的退出代码进行断言。为此，我们将使用 <code v-pre>artisan</code> 方法从我们的测试中调用 Artisan 命令。然后，我们将使用 <code v-pre>assertExitCode</code> 方法断言该命令以给定的退出代码完成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 测试控制台命令。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_console_command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>assertNotExitCode</code> 方法断言命令没有以给定的退出代码退出：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertNotExitCode</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，所有终端命令通常在成功时以 <code v-pre>0</code> 状态码退出，在不成功时以非零退出码退出。因此，为方便起见，你可以使用 <code v-pre>assertSuccessful</code> 和 <code v-pre>assertFailed</code> 断言来断言给定命令是否以成功的退出代码退出：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertSuccessful</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'inspire'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertFailed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="期望输入-输出" tabindex="-1"><a class="header-anchor" href="#期望输入-输出"><span>期望输入/输出</span></a></h2>
<p>Laravel 允许你使用 <code v-pre>expectsQuestion</code> 方法轻松 「mock」控制台命令的用户输入。此外，你可以使用 <code v-pre>assertExitCode</code> 和 <code v-pre>expectsOutput</code> 方法指定你希望通过控制台命令输出的退出代码和文本。例如，考虑以下控制台命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'question'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">ask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$language</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Which language do you prefer?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Python'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is '</span><span class="token operator">.</span><span class="token variable">$name</span><span class="token operator">.</span><span class="token string single-quoted-string">' and you prefer '</span><span class="token operator">.</span><span class="token variable">$language</span><span class="token operator">.</span><span class="token string single-quoted-string">'.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以用下面的测试来测试这个命令，该测试利用了 <code v-pre>expectsQuestion</code>、<code v-pre>expectsOutput</code>、<code v-pre>doesntExpectOutput</code>、<code v-pre>expectsOutputToContain</code>、<code v-pre>doesntExpectOutputToContain</code> 和 <code v-pre>assertExitCode</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 测试控制台命令。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_console_command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'question'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsQuestion</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsQuestion</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Which language do you prefer?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is Taylor Otwell and you prefer PHP.'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">doesntExpectOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your name is Taylor Otwell and you prefer Ruby.'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">expectsOutputToContain</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">doesntExpectOutputToContain</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'you prefer Ruby'</span><span class="token punctuation">)</span></span>
<span class="line">         <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="确认期望" tabindex="-1"><a class="header-anchor" href="#确认期望"><span>确认期望</span></a></h4>
<p>当编写一个期望以「是」或「否」答案形式确认的命令时，你可以使用 <code v-pre>expectsConfirmation</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'module:import'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">expectsConfirmation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Do you really wish to run this command?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'no'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">assertExitCode</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="表格期望" tabindex="-1"><a class="header-anchor" href="#表格期望"><span>表格期望</span></a></h4>
<p>如果你的命令使用 Artisan 的 <code v-pre>table</code> 方法显示信息表，则为整个表格编写输出预期会很麻烦。相反，你可以使用 <code v-pre>expectsTable</code> 方法。此方法接受表格的标题作为它的第一个参数和表格的数据作为它的第二个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">artisan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users:all'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">expectsTable</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'ID'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Email'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'abigail@example.com'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/console-tests/14897" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/co...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/console-tests/14897" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/co...</a></p>
</blockquote>
</div></template>


